<?php

namespace app\admin\controller\workteam;

use app\admin\model\AuthGroup;
use app\common\controller\Backend;
use fast\Tree;
use think\Config;

/**
 * 工作台
 */
class Dashboard extends Backend
{

    protected $noNeedRight = ['mytask','myproject'];
    public function _initialize()
    {
        parent::_initialize();

    }

    /**
     * 查看
     */
    public function index()
    {
        $taskModel=new  \app\admin\model\workteam\Task();

        $thismonth_start = mktime(0, 0, 0, date("m"), 1, date("Y"));
        $thismonth_end = mktime(23, 59, 59, date("m"), date("t"), date("Y"));
        $lastmonth_start = mktime(0, 0 , 0,date("m")-1,1,date("Y"));
        $lastmonth_end = mktime(23,59,59,date("m") ,0,date("Y"));
        //本月任务完成工时
        $thismonth_hours=$taskModel->where('admin_id',$this->auth->id)
            ->where('complete_time','between time',[$thismonth_start,$thismonth_end])
            ->where('status',2)->sum('get_hours');

        //上个月任务完成工时
        $lastmonth_hours=$taskModel->where('admin_id',$this->auth->id)
            ->where('complete_time','between time',[$lastmonth_start,$lastmonth_end])
            ->where('status',2)->sum('get_hours');

        //本月任务完成数量
        $thismonth_num=$taskModel->where('admin_id',$this->auth->id)
            ->where('complete_time','between time',[$thismonth_start,$thismonth_end])
            ->where('status',2)->count();

        //上个月任务完成数量
        $lastmonth_num=$taskModel->where('admin_id',$this->auth->id)
            ->where('complete_time','between time',[$lastmonth_start,$lastmonth_end])
            ->where('status',2)->count();
        //逾期任务
        $overdue_num=$taskModel->where('admin_id',$this->auth->id)
            ->where('end_time','<',time())
            ->where('status','not in',[-1,2])->count();

        //待处理任务
        $todo_num=$taskModel->where('admin_id',$this->auth->id)
            ->where('start_time','<',time())
            ->where('status','not in',[-1,2])->count();



        $erchart = [
            'hours' => $this->myhours(false),
        ];
        $this->view->assign([
            'erchart' => $erchart,
        ]);

        $this->assignconfig("statusList", $taskModel->getStatusList());
        $this->assignconfig("levelList", $taskModel->getLevelList());

        $this->view->assign([
            'thismonth_hours'=>$thismonth_hours,
            'lastmonth_hours'=>$lastmonth_hours,
            'thismonth_num'=>$thismonth_num,
            'lastmonth_num'=>$lastmonth_num,
            'overdue_num'=>$overdue_num,
            'todo_num'=>$todo_num,
        ]);
        return $this->view->fetch();
    }

    /**
     * 我的任务
     * @internal
     */
    public function mytask(){
        $this->model = new  \app\admin\model\workteam\Task();
        $filter_w ['admin_id']= $this->auth->id;
        //如果发送的来源是Selectpage，则转发到Selectpage
        if ($this->request->request('keyField')) {
            $this->request->request(['custom' => $filter_w]);
            return parent::selectpage();
        }

        list($where, $sort, $order, $offset, $limit) = $this->buildparams();
        $total = $this->model
            ->where($where)
            ->where($filter_w)
            ->order($sort, $order)->fetchSql(false)
            ->count();
        $list = $this->model
            ->where($where)
            ->with(['admin' => function ($q) {
                $q->field('id,nickname');
            },
                'project' => function ($q) {
                    $q->field('id,name');
                }])
            ->where($filter_w)
            ->order($sort, $order)
            ->limit($offset, $limit)->fetchSql(false)
            ->select();
        $result = array("total" => $total, "rows" => $list);
        return json($result);
    }
    /**
     * 我的任务
     * @internal
     */
    public function myproject(){
        $this->model = new  \app\admin\model\workteam\Project();
        $filter_w ['project_admin_id']= $this->auth->id;
        //如果发送的来源是Selectpage，则转发到Selectpage
        if ($this->request->request('keyField')) {
            $this->request->request(['custom' => $filter_w]);
            return parent::selectpage();
        }

        list($where, $sort, $order, $offset, $limit) = $this->buildparams();
        $total = $this->model
            ->where($where)
            ->where($filter_w)
            ->order($sort, $order)->fetchSql(false)
            ->count();
        $list = $this->model
            ->where($where)
            ->where($filter_w)
            ->order($sort, $order)
            ->limit($offset, $limit)->fetchSql(false)
            ->select();
        $result = array("total" => $total, "rows" => $list);
        return json($result);
    }


    /**
     * 我的工时
     * @internal
     */
    public function myhours($is_ajax = true){
        $this->model = new  \app\admin\model\workteam\Task();
        $startDate = $this->request->param('start_date', null);
        $endDate = $this->request->param('end_date', null);
        $data=$this->model->getHoursEchart($startDate,$endDate,$this->auth->id);
        if ($is_ajax) {
            $this->success('', '', [
                'hours' => $data
            ]);
        } else {
            return $data;
        }
    }

    /**
     * 添加
     * @internal
     */
    public function add()
    {
        $this->error();
    }

    /**
     * 编辑
     * @internal
     */
    public function edit($ids = null)
    {
        $this->error();
    }

    /**
     * 删除
     */
    public function del($ids = "")
    {
        if (!$this->request->isPost()) {
            $this->error(__("Invalid parameters"));
        }
        $ids = $ids ? $ids : $this->request->post("ids");
        if ($ids) {
            $adminList = $this->model->where('id', 'in', $ids)->select();
            if ($adminList) {
                $deleteIds = [];
                foreach ($adminList as $k => $v) {
                    $deleteIds[] = $v->id;
                }
                if ($deleteIds) {
                    $this->model->destroy($deleteIds);
                    $this->success();
                }
            }
        }
        $this->error();
    }

    /**
     * 批量更新
     * @internal
     */
    public function multi($ids = "")
    {
        // 管理员禁止批量操作
        $this->error();
    }


}
